using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SMD2XX;

namespace SelectDevice
{
 
    // 1 opens by serial number using FT_OpenEX
    // 2 opens by description using FT_OpenEX
    // 3 opens by Location Id using FT_OpenEX
    // 4 opens by device number using FT_Open
    public enum OPENBY
    {
        SERIAL_NUMBER = 1,
        DESCRIPTION,
        LOCATIONID,
        DEVICE_NUMBER
    }

    public partial class Select_Device : Form
    {
        private FT_STATUS ftStatus; // create an instance the FT_STATUS enum

        SM_D2XX SM = new SM_D2XX();

        public Select_Device()
        {
            InitializeComponent();
        }

        private void SelectAndOpenDevice_Load_1(object sender, EventArgs e)
        {
            ftStatus = SM.createDeviceInfoList();
            if (ftStatus != FT_STATUS.FT_OK)
            {
                MessageBox.Show("Error: buttonGet_Click FT_CreateDeviceInfoList failed.");
                return;
            }

            comboBoxSerialNumber.Items.Clear();
            comboBoxDescription.Items.Clear();
            comboBoxLocID.Items.Clear();


            richTextBox1.Text = "Numer of devices = " + SM.NumDevs.ToString() + "\n";

            for (UInt32 n = 0; n < SM.NumDevs; n++)
            {
                SM.getDeviceInfoDetail(n);

                serialNumber_p = SM.SerialNumber;
                description_p = SM.Description;
                locID_p = SM.LocID;

                if (ftStatus != FT_STATUS.FT_OK)
                {
                    MessageBox.Show("Error: GetDeviceInfoDetail failed.");
                    return;
                }
                else
                {
                    comboBoxSerialNumber.Items.Add(serialNumber_p);
                    comboBoxDescription.Items.Add(description_p);
                    comboBoxLocID.Items.Add(locID_p);
                    richTextBox1.Text += "\nSerialNumber = " + serialNumber_p;
                    richTextBox1.Text += "\nDescription = " + description_p;
                    richTextBox1.Text += "\nLocID = " + locID_p.ToString() + "\n";            
                } 
            }
        }


        #region Properties

        private string serialNumber_p = "NOT INITIALIZED";
        public string SerialNumber { get { return serialNumber_p; } set { serialNumber_p = value; } }

        private string description_p = "NOT INITIALIZED";
        public string Description { get { return description_p; } set { description_p = value; } }

        private UInt32 locID_p;
        public UInt32 LocID { get { return locID_p; } set { locID_p = value; } }

        private UInt32 index_p;
        public UInt32 Index { get { return index_p; } set { index_p = value; } }
        
        private UInt32 openBy_p;
        public UInt32 OpenBy { get { return openBy_p; } set { openBy_p = value; } } 

        #endregion

        #region Select Device to Open
        private void comboBoxSerialNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            openBy_p = (UInt32)OPENBY.SERIAL_NUMBER;
            serialNumber_p = comboBoxSerialNumber.SelectedItem.ToString();
            index_p = (uint)comboBoxSerialNumber.SelectedIndex;
            Close(); 
        }

        private void comboBoxDescription_SelectedIndexChanged(object sender, EventArgs e)
        {
            openBy_p = (UInt32)OPENBY.DESCRIPTION;
            description_p = comboBoxDescription.SelectedItem.ToString();
            index_p = (uint)comboBoxDescription.SelectedIndex;
            Close();
        }

        private void comboBoxLocID_SelectedIndexChanged(object sender, EventArgs e)
        {
            openBy_p = (UInt32)OPENBY.LOCATIONID;
            locID_p = UInt32.Parse(comboBoxLocID.SelectedItem.ToString());
            index_p = (uint)comboBoxLocID.SelectedIndex;
            Close();
        }
        #endregion


    }
}